import * as vscode from 'vscode';
import { StreamInlineEdit } from '../../../src/api/streamInlineEdit';
import { BaseLLM } from "../../../core/llm/baseLLM"
import { DeepSeek } from "../../../core/llm/deepSeek"
import { ChatMessage } from '../../../core';
import { VerticalDiffManager } from '../../../src/diff/vertical/manager';
import { LLMFactory } from '../../../core/llm/llmFactory';


function getRange(stratLine0:number, endLine0:number): vscode.Range {
    const startLine = Math.min(stratLine0, endLine0);
    const endLine = Math.max(stratLine0, endLine0);
    return new vscode.Range(startLine, 0, endLine, Number.MAX_SAFE_INTEGER);
}


export function addComment(verticalDiffManager: VerticalDiffManager, llmFactory: LLMFactory) {
    // 调用api得到llm，可直接使用，llm-Name区分实例化
    let llm: BaseLLM|undefined = llmFactory.getLLM("deepseek-chat");
    if (!llm) {
        vscode.window.showErrorMessage("LLM not found");
        return;
    }

    let editor = vscode.window.activeTextEditor;
    if (!editor) {
        return undefined;
    }
    let startLine, endLine: number;
    startLine = editor.selection.start.line;
    endLine = editor.selection.end.line;
    let selectedRange = getRange(startLine, endLine);
    let highlighted = editor.document.getText(selectedRange);


    const prompt: ChatMessage[] = [
        {role: "user", content: "write comments for this code"},
        {role: "user", content: highlighted},
      ];

    verticalDiffManager.streamEdit(llm._streamChat(prompt, new AbortController().signal));
}